package com.tuo51.evaluate.repositories.children;

import com.tuo51.evaluate.domain.childrens.Children;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.tuo51
 * on 2017/7/13
 * *******************************
 */
public interface ChildrenRepository extends JpaRepository<Children, String>, JpaSpecificationExecutor<Children> {

    Children getByNameAndStaffIdAndDeletedIsFalse(String name, String staffId);

    @Query(value = "select * from children c where c.id in (" +
            "select childrenId from children_extend_info e where e.guardianPhone = :guardianPhone" +
            ") and c.deleted is false", nativeQuery = true)
    List<Children> findAllByParentPhoneAndDeletedIsFalse(@Param("guardianPhone") String phone);

    Children getByIdAndStaffId(String childrenId, String staffId);

    List<Children> findByStaffIdAndDeletedIsFalseOrderByCreatedDesc(String staffId);
    List<Children> findByStaffIdAndDeletedIsFalseOrderByBirthdayDesc(String staffId);
    List<Children> findByStaffIdAndDeletedIsFalseOrderByNextEvaluateDateDesc(String staffId);
    List<Children> findByStaffIdAndDeletedIsFalseOrderByLastEvaluateDateDesc(String staffId);

    List<Children> findByStaffIdAndNameLikeAndDeletedIsFalseOrderByCreatedDesc(String staffId, String name);
    List<Children> findByStaffIdAndNameLikeAndDeletedIsFalseOrderByBirthdayDesc(String staffId, String name);
    List<Children> findByStaffIdAndNameLikeAndDeletedIsFalseOrderByNextEvaluateDateDesc(String staffId, String name);
    List<Children> findByStaffIdAndNameLikeAndDeletedIsFalseOrderByLastEvaluateDateDesc(String staffId, String name);


    List<Children> findByIdIn(String... childrenIds);


    int countAllByStaffIdInAndCreatedGreaterThanAndDeletedIsFalse(List<String> ids, Date date);

    int countAllByStaffIdInAndDeletedIsFalse(List<String> ids);

    //迁移报告
    @Modifying
    @Query(value = "update children r set r.staffId = ?2, r.orgId = ?3 where r.staffId = ?1 ", nativeQuery = true)
    void updateAllChildrenFromTo(String sourceTeacherId, String targetTeacherId, String tagetOrgId);
}
